package com.lagou.edu.controller;

import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author dell
 * @date 2020/9/28 11:10
 * @description
 */
@Controller
@RequestMapping("/resume")
public class ResumeController {

    /**
     * 测试验证拦截
     * @return
     */
    @RequestMapping("/handle01")
    public ModelAndView handle01(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("date",System.currentTimeMillis());
        modelAndView.setViewName("index");
        return modelAndView;

    }


    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/showAll")
    public String show(Model model){
        List<Resume> allResume = resumeService.findAll();
        model.addAttribute("allResume",allResume);
        return "info";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Resume resume) {
        int i = resumeService.updateOne(resume);
        if(i>0){
            return "redirect:/resume/showAll";
        }else{
            return "update";
        }
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addOne(Resume resume) {
            return "update";
    }

    @RequestMapping("/updateById")
    public String toUpdate(Model model,long id){
        if(id!=-1){
            Resume resume = resumeService.findById(id);
            model.addAttribute("currResume",resume);
        }
        return "update";
    }

    @RequestMapping("/deleteById")
    public String deleteOne(long id){
        resumeService.deleteOne(id);
        return "redirect:/resume/showAll";
    }
}
